package elitech.vietnam.weatherfashion.tabs.weekly;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import elitech.vietnam.weatherfashion.MainActivity;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.tabs.AbstractFragment;
import elitech.vietnam.weatherfashion.ultilities.Utilities;
import elitech.vietnam.weatherfashion.weatherservice.WeatherDayObject;
import elitech.vietnam.weatherfashion.weatherservice.WeatherObject;
import elitech.vietnam.weatherfashion.widget.NotifyButton;

public class WeeklyFragment extends AbstractFragment implements View.OnClickListener {

	private FragmentTabHost mTabHost;
	private TextView mHeader;
	private NotifyButton button_notification;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.weekly, null);
		setupView(view);

		mTabHost = (FragmentTabHost) view.findViewById(R.id.weekly_tabhost);
		mHeader = (TextView) view.findViewById(R.id.weekly_text_header);
		button_notification = (NotifyButton) view.findViewById(R.id.button_notification);
		
		((MainActivity) getActivity()).setCurrentNotifyButton(button_notification);

		if (!isAdded())
			return null;

		MemberDTO m = GlobalValue.getInstance(getActivity()).readUserInfo();
		if (m == null)
			button_notification.setVisibility(View.GONE);
		else 
			button_notification.setVisibility(View.VISIBLE);
		//button_notification.setOnClickListener(this);

		mHeader.setText(Html.fromHtml(getString(R.string.weeklytitle)));
		initTabs();

		return view;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (getActivity().getIntent().getIntExtra("WEEKLY_FIRST_TAB", -1) > 0) {
			mTabHost.setCurrentTab(0);
			getActivity().getIntent().removeExtra("WEEKLY_FIRST_TAB");
		}
	}

	public void initTabs() {
		mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.weekly_realtabcontent);
		for (int i = 0; i < 5; i++) {
			int normal = R.drawable.tab_icon_trong, selected = normal;
			WeatherObject weather = GlobalValue.getInstance(getActivity()).readWeatherInfo();

			if (weather != null && weather.getListDay() != null) {
				if (weather.getListDay().size() > i + 1) {
					WeatherDayObject day;
					if (i == 0)
						day = weather.getToday();
					else
						day = weather.getListDay().get(i - 1);
					selected = Utilities.getTabIconDrawableFromTypeOfWeather(getActivity(), day.getType());
					normal = Utils.getDateDrawable(day.getDay());
				}
			} else {
				normal = Utils.getDateDrawable(i);
				selected = normal;
			}

			TabSpec tab = mTabHost.newTabSpec(String.valueOf(i)).setIndicator(createTabView(getActivity(), selected, normal));
			Bundle bundle = new Bundle();
			bundle.putInt("index", i);
			mTabHost.addTab(tab, WeeklyChildFragment.class, bundle);
		}
	}

	private static View createTabView(final Context context, int selected, int normal) {
		View view = LayoutInflater.from(context).inflate(R.layout.weekly_tab, null);
		Drawable drawable = Utils.getTabDrawable(context, selected, normal);
		ImageView item_image = (ImageView) view.findViewById(R.id.weekly_childtab_itemimage);
		item_image.setImageDrawable(drawable);
		return view;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		
		case R.id.button_notification:
			Log.w("Notification", "Num Count: " + WFApplication.getInstance().mListNoti.size());
			((MainActivity) getActivity()).mNotiWindow.showAsDropDown(button_notification);
			((MainActivity) getActivity()).mNotiWindow.notifyDataChanged();
			button_notification.setNumber(0);	
			break;
		
		default:
			break;
		}
	}
}
